Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
"can't access variable before initialization" if I break the code into separate function

I've found several articles, both here on SO and elsewhere that mention this error message, but none that I can apply to my specific situation. Can anyone help me understand what's going on?

If I write my code like so:

selDocument.group_selected = function ($, anchor) {
    let tmpitems = $(".chkSelectedField:checked");
    if (tmpitems.length > 1) {
        let items = [];
        $(tmpitems).each(function (a, b) {
            items.push($(b).val());
        });
        if (typeof anchor != 'undefined') {
            items = items.filter(el => el != anchor);
        } else {
            anchor = items.shift();
        }
        console.log("57199516 items, anchor", items, anchor);

it works fine, but if I take those blocks and break them into their own function:

selDocument.group_selected = function ($, anchor) {
        let tmpitems = $(".chkSelectedField:checked");
        if (tmpitems.length > 1) {
            let retval = selDocument.create_items_array($, tmpitems, anchor);
            let items = retval.items;
            anchor = retval.anchor;
            console.log("57199516 items, anchor", items, anchor);
...
selDocument.create_items_array = function ($, tmpitems, anchor) {
    let items = [];
    $(tmpitems).each(function (a, b) {
        items.push($(b).val());
    });
    if (typeof anchor != 'undefined') {
        items = items.filter(el => el != anchor);
    } else {
        anchor = items.shift();
    }
    return JSON.parse({ 'items': items, 'anchor': anchor });
};

I get "Cannot access 'anchor' before initialization."

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda